package com.jorado.metrics.util;

public class LabelHelp {

    protected static String[] DEF_LABELS = new String[4];

    protected static String[] DEF_VALUES = new String[4];

    static {
        DEF_LABELS[0] = ("environment");
        DEF_LABELS[1] = ("app_name");
        DEF_LABELS[2] = ("host_name");
        DEF_LABELS[3] = ("local_ip");

        // appName
        String appName = "UNKNOWN_APPLICATION";
        String path = System.getProperty("user.dir");
        if (path.contains("/")) {
            String[] pathArray = path.split("/");
            if (pathArray.length > 1) {
                appName = pathArray[pathArray.length - 2];
            }
        }
        String property_appName = System.getProperty("APP_NAME");
        if (property_appName != null && !"".equals(property_appName)) {
            appName = property_appName;
        }

        // hostName
        String hostName = System.getenv("HOSTNAME");
        if (hostName == null) {
            hostName = "";
        }

        // cluster
        String cluster = System.getenv("CLUSTER");
        if (cluster == null) {
            cluster = "";
        }

        DEF_VALUES[0] = cluster;
        DEF_VALUES[1] = appName;
        DEF_VALUES[2] = hostName;
        DEF_VALUES[3] = IPResolver.getIP();
    }


    public static String[] addLabels() {
        return DEF_LABELS;
    }

    public static String[] addLabels(String... labels) {
        if (labels == null || labels.length <= 0) {
            return addLabels();
        } else {
            String[] c = new String[DEF_LABELS.length + labels.length];
            System.arraycopy(DEF_LABELS, 0, c, 0, DEF_LABELS.length);
            System.arraycopy(labels, 0, c, DEF_LABELS.length, labels.length);
            return c;
        }
    }

    public static String[] addValues() {
        return DEF_VALUES;
    }

    public static String[] addValues(String... values) {
        if (values == null || values.length <= 0) {
            return DEF_VALUES;
        } else {
            String[] c = new String[DEF_VALUES.length + values.length];
            System.arraycopy(DEF_VALUES, 0, c, 0, DEF_VALUES.length);
            for (int i = 0; i < values.length; i++) {
                if (values[i] != null) {
                    c[i + DEF_VALUES.length] = values[i];
                } else {
                    c[i + DEF_VALUES.length] = "";
                }
            }
            return c;
        }
    }

}
